package com.aistar.controller;

import com.aistar.pojo.Job;
import com.aistar.service.JobService;
import com.aistar.service.impl.JobServiceImpl;
import com.aistar.util.DateUtil;
import com.aistar.util.ServerResponse;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "JobServlet",urlPatterns = "/job")
public class JobServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        switch (op){
            case "getlatest" :getLatest(request,response);break;
            case "all" :getAll(request,response);break;
            case "detail" :detail(request,response);break;
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
    private  void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获得页面查询的参数


        String kw =  request.getParameter("kw");
        String city =  request.getParameter("city");
        String publishtimeStr =  request.getParameter("publishtime"); //-1,-3,-7,-30
        String minsalaryStr =  request.getParameter("minsalary");
        String maxsalaryStr =  request.getParameter("maxsalary");
        String workyear =  request.getParameter("workyear");//null
        String degree =  request.getParameter("degree");
        String company =  request.getParameter("company");

        if(kw==null || kw.trim().equals(""))
            kw = null;
        if(city==null || city.trim().equals(""))
            city = null;

        //发布日期
        Date publishtime = null;
        if(publishtimeStr!=null && !publishtimeStr.equals(""))
            publishtime = DateUtil.getBeforeDate(Integer.valueOf(publishtimeStr));

        //最低薪资 最高薪资
        Float minSalary =0f;
        Float maxSalary =50000f;
        if(minsalaryStr!=null && !minsalaryStr.equals(""))
            minSalary = Float.valueOf(minsalaryStr);

        if(maxsalaryStr!=null && !maxsalaryStr.equals(""))
            maxSalary = Float.valueOf(maxsalaryStr);

        //学历
        if(degree ==null || degree.trim().equals(""))
            degree =null;
        //工作年限
        if(workyear ==null || workyear.trim().equals(""))
            workyear =null;

        //页码
        String pageNumStr = request.getParameter("page");
        Integer pageNum ;
        if(pageNumStr == null)
            pageNum =1;
        else
            pageNum = Integer.valueOf(pageNumStr);

        //3. service
        JobService service =  new JobServiceImpl();
        ServerResponse serverResponse = service.getAll(kw,city,publishtime,minSalary,maxSalary,workyear,degree,company,pageNum);

      //4. 返回ServerResponse (data: pageInfo<JobCompanyInfo>)
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));

    }

    private   void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String jobIdStr = request.getParameter("id");
       Integer jobId = Integer.valueOf(jobIdStr);
      JobService service =  new JobServiceImpl();
      ServerResponse serverResponse = service.getById(jobId);

      request.setAttribute("jobCompany",serverResponse.getData());
      request.getRequestDispatcher("/job/detail.jsp").forward(request,response);

    }

        private   void getLatest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       JobService service =  new JobServiceImpl();
      ServerResponse serverResponse =  service.getSomeLatest();
       response.setContentType("text/html;charset=UTF-8");
       response.getWriter().println(JSON.toJSONString(serverResponse));

   }

}
